package org.mat.ipaas.ucenter.bff.admin.modules.system;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.mat.framework.lang.dto.MatApiResponse;
import org.mat.ipaas.ucenter.service.api.system.SysDepartPermissionRestAPI;
import org.mat.ipaas.ucenter.service.dto.system.entity.SysDepartPermissionDTO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 部门权限表
 */
@Slf4j
@Api(tags = "部门权限表")
@RestController
@RequestMapping("/sys/sysDepartPermission")
public class SysDepartPermissionController {

    private SysDepartPermissionRestAPI sysDepartPermissionRestAPI;

    /**
     * 分页列表查询
     *
     * @param sysDepartPermissionDTO
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "部门权限表-分页列表查询", notes = "部门权限表-分页列表查询")
    @GetMapping(value = "/list")
    public MatApiResponse<?> queryPageList(SysDepartPermissionDTO sysDepartPermissionDTO,
                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        MatApiResponse<?> matApiResponse = sysDepartPermissionRestAPI.queryPageList(sysDepartPermissionDTO, pageNo, pageSize);
        return matApiResponse;
    }

    /**
     * 添加
     *
     * @param sysDepartPermissionDTO
     * @return
     */
    @ApiOperation(value = "部门权限表-添加", notes = "部门权限表-添加")
    @PostMapping(value = "/add")
    public MatApiResponse<?> add(@RequestBody SysDepartPermissionDTO sysDepartPermissionDTO) {
        MatApiResponse<?> matApiResponse = sysDepartPermissionRestAPI.add(sysDepartPermissionDTO);
        return matApiResponse;
    }

    /**
     * 编辑
     *
     * @param sysDepartPermissionDTO
     * @return
     */
    @ApiOperation(value = "部门权限表-编辑", notes = "部门权限表-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public MatApiResponse<?> edit(@RequestBody SysDepartPermissionDTO sysDepartPermissionDTO) {
        MatApiResponse<?> matApiResponse = sysDepartPermissionRestAPI.edit(sysDepartPermissionDTO);
        return matApiResponse;
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "部门权限表-通过id删除", notes = "部门权限表-通过id删除")
    @DeleteMapping(value = "/delete")
    public MatApiResponse<?> delete(@RequestParam(name = "id", required = true) String id) {
        MatApiResponse<?> matApiResponse = sysDepartPermissionRestAPI.delete(id);
        return matApiResponse;
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "部门权限表-批量删除", notes = "部门权限表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public MatApiResponse<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        MatApiResponse<?> matApiResponse = sysDepartPermissionRestAPI.deleteBatch(ids);
        return matApiResponse;
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "部门权限表-通过id查询", notes = "部门权限表-通过id查询")
    @GetMapping(value = "/queryById")
    public MatApiResponse<SysDepartPermissionDTO> queryById(@RequestParam(name = "id", required = true) String id) {
        MatApiResponse<SysDepartPermissionDTO> matApiResponse = sysDepartPermissionRestAPI.queryById(id);
        return matApiResponse;
    }

    /**
     * 导出excel
     *
     * @param sysDepartPermissionDTO
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(SysDepartPermissionDTO sysDepartPermissionDTO) {
        ModelAndView modelAndView = sysDepartPermissionRestAPI.exportXls(sysDepartPermissionDTO);
        return modelAndView;
    }

    /**
     * 通过excel导入数据
     *
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public MatApiResponse<?> importExcel() {
        MatApiResponse<?> matApiResponse = sysDepartPermissionRestAPI.importExcel();
        return matApiResponse;
    }

    /**
     * 部门管理授权查询数据规则数据
     */
    @GetMapping(value = "/datarule/{permissionId}/{departId}")
    public MatApiResponse<?> loadDatarule(@PathVariable("permissionId") String permissionId, @PathVariable("departId") String departId) {
        MatApiResponse<?> matApiResponse = sysDepartPermissionRestAPI.loadDatarule(permissionId, departId);
        return matApiResponse;
    }

    /**
     * 保存数据规则至部门菜单关联表
     */
    @PostMapping(value = "/datarule")
    public MatApiResponse<?> saveDatarule(@RequestBody JSONObject jsonObject) {
        MatApiResponse<?> matApiResponse = sysDepartPermissionRestAPI.saveDatarule(jsonObject);
        return matApiResponse;
    }

    /**
     * 查询角色授权
     *
     * @return
     */
    @RequestMapping(value = "/queryDeptRolePermission", method = RequestMethod.GET)
    public MatApiResponse<List<String>> queryDeptRolePermission(@RequestParam(name = "roleId", required = true) String roleId) {
        MatApiResponse<List<String>> matApiResponse = sysDepartPermissionRestAPI.queryDeptRolePermission(roleId);
        return matApiResponse;
    }

    /**
     * 保存角色授权
     *
     * @return
     */
    @RequestMapping(value = "/saveDeptRolePermission", method = RequestMethod.POST)
    public MatApiResponse<String> saveDeptRolePermission(@RequestBody JSONObject json) {
        MatApiResponse<String> matApiResponse = sysDepartPermissionRestAPI.saveDeptRolePermission(json);
        return matApiResponse;
    }

    /**
     * 用户角色授权功能，查询菜单权限树
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryTreeListForDeptRole", method = RequestMethod.GET)
    public MatApiResponse<Map<String, Object>> queryTreeListForDeptRole(@RequestParam(name = "departId", required = true) String departId, HttpServletRequest request) {
        MatApiResponse<Map<String, Object>> matApiResponse = sysDepartPermissionRestAPI.queryTreeListForDeptRole(departId, request);
        return matApiResponse;
    }

}
